﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;
using System.ComponentModel.Design;

using DotSpatial.Topology;

using DotSpatial.Symbology;
using DotSpatial.Data;

namespace HAGISV2
{

    [Serializable]
    /// <summary>
    /// 用于表示所有的图层属性底层，付志辉
    /// </summary>
    public abstract class Layer
    {
        #region 构造函数

        public Layer(string id)
        {
            this._id = id;
            this._desc = id;

        }


        //有空再提供更多的构造函数

        //public Layer(string id,string desc)
        //{

        //}


        #endregion

        #region 私有字段

        string _id;



        string _desc;



        #endregion

        #region 受保护的字段

        protected FMAPVIEW _viewMode = FMAPVIEW.NORMAL;

        /// <summary>
        /// 图层是否已经被加载
        /// </summary>
        protected bool _isLoaded = false;


        protected bool _isReadyRedraw = true;
        

        protected bool _visiable = true;

        protected float _opacity = 1.0F;


        protected bool _isSelectable = false;


        protected Layer _parent;





        [NonSerialized]
        protected GisManager _defaultManager;




        #endregion

        #region 公共属性

        /// <summary>
        /// 该图层包含的gis图层数量
        /// </summary>
        public virtual int MapLayerCount
        {
            get { return 1; }
        }

        public Layer Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }



        public FMAPVIEW ViewMode
        {
            get { return _viewMode; }
            set
            {
                if (_viewMode != value)
                {
                    _viewMode = value;
                    OnViewModeChanged();
                }
            }
        }



        [Browsable(false)]
        /// <summary>
        /// 图层ID
        /// </summary>
        public string Id
        {
            get { return _id; }

        }

        protected int _mapLayerIndex = -1;
        /// <summary>
        /// 图层渲染时使用
        /// gis图层插入标志如果是-1就直接添加，否则将图层插入到该位置 
        /// </summary>
        public int MapLayerIndex
        {
            set { _mapLayerIndex = value; }
        }

        /// <summary>
        /// 图层描述名称
        /// </summary>
        public string Desc
        {
            get { return _desc; }
            set { _desc = value; }
        }


        [Browsable(false)]
        public GisManager DefaultManager
        {
            get { return _defaultManager; }
            set
            {
                _defaultManager = value;


                this.OnSetGisManager();

            }
        }


        /// <summary>
        /// 是否已经加载
        /// </summary>
        [Browsable(false)]
        public bool IsLoaded
        {
            get { return _isLoaded; }

        }


        /// <summary>
        /// 是否允许立即刷新，用于阻塞不必要的刷新过程
        /// </summary>
        [Browsable(false)]
        protected bool IsReadyRedraw
        {
            get { return _isReadyRedraw; }

        }


        /// <summary>
        /// 图层是否可见
        /// </summary>
        [Category("图层设置"), DisplayName("图层是否可见"), Browsable(false)]
        public bool Visible
        {
            get { return this._visiable; }
            set
            {
                this._visiable = value;
                this.OnSetVisible();
            }
        }



        /// <summary>
        /// 图层透明度
        /// </summary>
        [Category("图层设置"), DisplayName("图层透明度")]
        public virtual float Opacity
        {
            get { return _opacity; }
            set
            {

                if (value > 1.0F)
                {
                    value = 1.0F;
                }
                else if (value < 0.0F)
                {
                    value = 0.0F;
                }

                this._opacity = value;
                this.OnSetLayerOpacity();


            }
        }





        public bool IsSelectable
        {
            get { return _isSelectable; }
            set
            {
                _isSelectable = value;
                this.OnSetSelectable();
            }
        }


        #endregion

        #region 受保护的方法

        /// <summary>
        /// 切换视图触发
        /// </summary>
        protected virtual void OnViewModeChanged()
        {

        }


        /// <summary>
        /// 图层透明度
        /// </summary>
        protected virtual void OnSetLayerOpacity()
        {
            if (this._isLoaded == false) return;
        }


        /// <summary>
        /// 图层可见性
        /// </summary>
        protected virtual void OnSetVisible()
        {


        }

        /// <summary>
        /// 图层可选择性
        /// </summary>
        protected virtual void OnSetSelectable()
        {
            //if (_isLoaded)
            //{
            //    this.ILayer.SelectionEnabled = this.m_bSelectable;
            //}

        }


        protected virtual void OnSetGisManager()
        {


        }


        #endregion

        #region 公共方法

        /// <summary>
        /// 将自身添加到map进行渲染
        /// </summary>
        public virtual void Render()
        {


        }

        /// <summary>
        /// 获取图层自身范围 
        /// 如图层中图形空则返回值为null
        /// </summary>
        /// <returns></returns>
        public virtual Extent GetLayerExtent()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 将图层取出map,停止渲染
        /// </summary>
        public virtual void StopRender()
        {

        }







        /// <summary>
        /// 打开图层文件，并渲染之
        /// </summary>
        /// <returns></returns>
        public virtual bool OpenLayer()
        {
            return true;
        }


        /// <summary>
        /// 保存图层文件
        /// </summary>
        /// <returns></returns>
        public virtual bool SaveLayer()
        {
            return true;
        }


        /// <summary>
        /// 开始编辑属性，而不允许刷新界面
        /// </summary>

        public void StartEdit()
        {
            this._isReadyRedraw = false;
        }


        /// <summary>
        /// 结束属性编辑，并立即刷新界面显示
        /// </summary>
        public void EndEdit()
        {
            this._isReadyRedraw = true;

            this.Update();
        }




        /// <summary>
        /// 对外刷新函数，刷新图层的显示
        /// </summary>
        public void Update()
        {
            if (this._isLoaded == false) return;
            if (this.IsReadyRedraw)
            {

                this.Redraw();

                //刷新视图控件
                this.DefaultManager.CurrentMap.Invalidate();


            }



        }


        /// <summary>
        /// 强制重新绘制自身的所有元素
        /// </summary>
        public virtual void Redraw()
        {


        }


        #region 选择相关


        public virtual void Select(IEnvelope tolerant, IEnvelope strict, SelectionMode selectionMode)
        {





        }

        public virtual void ClearSelection()
        {

        }

        #endregion


        #endregion

    }
}
